<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻页面</title>
    <script src="./jquery/jquery3.1.1.js"></script>
    <!-- 引入bootstrap，需要两个，分别是：css，js（js引入的时候先引入jquery） -->
    <script src="./bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
    <script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>

    <style>
        /* 基础样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            color: #333;
        }

        header {
            background-color: #4CAF50;
            color: white;
            padding: 1rem;
            text-align: center;
        }

        .header h1 {
            margin: 0;
        }

        main {
            padding: 1rem;
        }

        .news-container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .news-container h2 {
            margin: 1rem 0;
            font-size: 1.5rem;
            color: #333;
            cursor: pointer;
        }

        .news-container h2:hover {
            color: #4CAF50;
        }

    </style>

</head>
<body>
<header>
    <h1>今日新闻</h1>
</header>
<main>
    <div class="news-container">


        <!-- 可以继续添加更多新闻标题 -->
    </div>
    <button id="xxx" onclick="last()">上一页</button>
    <span id="currentPage"></span>
    <button id="yyy" onclick="next()">下一页</button>

</main>
</body>
</html>

<script>
window.onload = function(){
        initDate()
    }
    let object1=null;
    let pagenum=1;
function initDate() {

    $.ajax({
        url: "news/titles",
        type: "get",
        dataType: "json",
        contentType:"application/json",
        data:{
            "pagenum":pagenum,
        },
        success: function (data) {
            if (data.code == 200) {
                object1=data.data;
                console.log(object1[0].id)
                for (let i = 0; i < 5; i++) {
                    $(".news-container").append("<h2 onclick='newsdetail("+data.data[i].id+")'>" + data.data[i].title + "</h2>")


                }

            }

        }
    })
}
function next() {
    $(".news-container").empty();
    // pagenum+=5;
    // let p=pagenum+5;
    // if(p>object1.length){
    //     p=object1.length;
    // }
    // for(let i=pagenum;i<p;i++){
    //     console.log(object1[i].title)
    //     $(".news-container").append("<h2 onclick='newsdetail("+object1[i].id+")'>" + object1[i].title + "</h2>")
    // }
    pagenum++;
    $.ajax({
        url: "news/titles",
        type: "get",
        dataType: "json",
        contentType:"application/json",
        data:{
            "pagenum":pagenum,
        },
        success: function (data) {
            if (data.code == 200) {
                object1=data.data;
                console.log(object1[0].id)
                for (let i = 0; i < 5; i++) {
                    $(".news-container").append("<h2 onclick='newsdetail("+data.data[i].id+")'>" + data.data[i].title + "</h2>")


                }

            }

        }
    })

}

function last() {
    $(".news-container").empty();

    pagenum--;
    if(pagenum<1){
        pagenum=1;
    }
    $.ajax({
        url: "news/titles",
        type: "get",
        dataType: "json",
        contentType:"application/json",
        data:{
            "pagenum":pagenum,
        },
        success: function (data) {
            if (data.code == 200) {
                object1=data.data;
                console.log(object1[0].id)
                for (let i = 0; i < 5; i++) {
                    $(".news-container").append("<h2 onclick='newsdetail("+data.data[i].id+")'>" + data.data[i].title + "</h2>")


                }

            }

        }
    })
}




























function newsdetail(id) {
    console.log(id)
    window.location.href="newsdetail.html?id="+id
}


</script>